package com.fingard.dsp.bank.directbank.lu11;

import java.io.File;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IStatement;
import com.fingard.dsp.bank.reqrespobj.statement.ReqQryStatement;
import com.fingard.dsp.bank.reqrespobj.statement.RetAtsStatement;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.dsp.bank.reqrespobj.statement.RefundState;
import com.fingard.dsp.bank.reqrespobj.statement.detail.RetAtsStatementDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

/**
 * 陆金所对账单
 * 
 * @author Cruise
 * @date 2019/6/4
 */
public class LU11Statement extends LU11Base implements IStatement {
	@Override
	public RetAtsStatement d_Qry_Statement(ReqQryStatement p_reqQrySttm) throws Exception {
		RetAtsStatement retAts = new RetAtsStatement(p_reqQrySttm);
		retAts.writeStart();

		String[] tmpSenderUnderHeader = getSenderUnderStatement(p_reqQrySttm, retAts);
		String tmpSenderUnderTotalNum = retAts.totalNum;
		if (retAts.getWrittenRdCount() == 0) {
			if ("-1".equals(tmpSenderUnderHeader[0])) {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo(tmpSenderUnderHeader[1]);
			} else {
				retAts.writeRespCode("0");
				retAts.writeRespInfo("交易为空");
			}
		} else {
			retAts.isWritedTotalNum(String.valueOf(Integer.parseInt(tmpSenderUnderTotalNum)));
		}

		retAts.writeEnd();
		return retAts;
	}

	/**
	 * 承保/退保对账单获取
	 * 
	 * @param p_reqQrySttm
	 * @param retAts
	 * @return
	 * @throws Exception
	 */
	public String[] getSenderUnderStatement(ReqQryStatement p_reqQrySttm, RetAtsStatement retAts) throws Exception {
		String[] respHeader = new String[] { "", "" };

		String respCode = "";
		String respInfo = "";

		retAts.totalNum = "0";
		int totalNum = 0;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_reqQrySttm.actNum, "senderUnder");
			if (actItem == null) {
				respHeader[0] = "-1";
				respHeader[1] = "未找到账号[" + p_reqQrySttm.actNum + "]相应商户配置信息!";
				return respHeader;
			}

			String tmpDownFileName = "";
			String tmpSendUnder = getBankConfig().getMapValueByKey("sendUnderMapping", p_reqQrySttm.actNum);
			if (StringHelper.isNullOrEmpty(tmpSendUnder)) {
				respHeader[0] = "-1";
				respHeader[1] = "'sendUnderMapping'节点配置为空,请检查config.xml相关配置!";
				return respHeader;
			}
			if ("underwrite".equals(tmpSendUnder)) {
				tmpSendUnder = "rec_underwrite";// 承保
			} else {
				tmpSendUnder = "pay_surrender";// 退保
			}
			tmpDownFileName = tmpSendUnder + "_" + Format.Date8Format.format(p_reqQrySttm.getBusDate()) + ".xml";
			String tmpResultFilePath = getNotifyDataDirectory(Format.Date8Format.format(p_reqQrySttm.getBusDate())) + File.separator + tmpDownFileName;
			WriteBankLogLn("待查询本机结果明细数据文件路径:\r\n" + tmpResultFilePath);

			String tmpRetStr = FileHelper.readFileText(tmpResultFilePath, getCharset());
			WriteBankLogLn2("对账单文件内容: \r\n" + tmpRetStr);

			StringBuilder tmpSbMsg = new StringBuilder();
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr, tmpSbMsg);
			while (tmpBkReader.read("/LU11/DetailRecord")) {
				totalNum++;
				RetAtsStatementDetail tmpRetDetail = new RetAtsStatementDetail();
				if ("rec_underwrite".equals(tmpSendUnder)) {
					tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleTextNotNull("AskNumber");
				} else {
					tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleTextNotNull("BkRefundSeq");

				}

				tmpRetDetail.cdFlag = tmpBkReader.readedXElement.getSingleTextNotNull("cdFlag");
				tmpRetDetail.amount = tmpBkReader.readedXElement.getSingleTextNotNull("Amount");
				tmpRetDetail.bkRefundSeq = tmpBkReader.readedXElement.getSingleTextNotNull("BkRefundSeq");
				tmpRetDetail.srcNote = tmpBkReader.readedXElement.getSingleTextNotNull("SrcNote");
				tmpRetDetail.bkSeq = tmpBkReader.readedXElement.getSingleTextNotNull("BkSeq");
				tmpRetDetail.busTime = tmpBkReader.readedXElement.getSingleTextNotNull("BusTime");
				tmpRetDetail.fee = tmpBkReader.readedXElement.getSingleTextNotNull("Fee") == null ? "0.00" : tmpBkReader.readedXElement.getSingleTextNotNull("Fee");
				tmpRetDetail.transState = TransState.Success;
				tmpRetDetail.refundState = RefundState.RefundPaying;
				retAts.writeRetDetail(tmpRetDetail);
			}
		} catch (Exception e) {
			e.printStackTrace();
			respCode = "-1";
			respInfo = e.getMessage();
			WriteBankLogLn(e);
		}

		retAts.totalNum = String.valueOf(totalNum);

		respHeader[0] = respCode;
		respHeader[1] = respInfo;

		return respHeader;
	}
}
